package cn.krui.project.service.system;

import cn.krui.helper.bean.DataArray;
import cn.krui.helper.bean.DataSingle;
import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.helper.tools.MapUtils;
import cn.krui.project.bean.system.sys_user.sys_user_info;
import cn.krui.project.controller.system.SystemBase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
/**
 * @program: project-system
 * @description: 用户模块
 * @author: admin
 * @create: 2019-10-11 10:57
 **/
@Service
public class SysUserService extends SystemBase {

	@Autowired
	private DBHelper dbHelper;

	/**
	 * 系统用户账户
	 */
	public sys_user_info selectSysUserbyUserName(sys_user_info sysUserInfo) {
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_users");
		sql.where("user_name='" + sysUserInfo.getUser_name() + "'");
		if (!StringUtils.isEmpty(sysUserInfo.getId())) {
			sql.where("id!=" + sysUserInfo.getId());
		}
		sys_user_info sysUserInfoone = dbHelper.selectOne(sql, sys_user_info.class);
		return sysUserInfoone;
	}

	/**
	 * 系统用户列表
	 * 
	 * @param sysUserInfo
	 * @return
	 */
	public DataArray<sys_user_info> selectSysUserList(DataSingle<sys_user_info> sysUserInfo) {
		SQL.SelectSQL sql = SQL.select("qu.*,qro.role_name,qdp.dept_name").from("qr_sys_users qu");
		sql.leftJoin("qr_sys_role qro", "qu.role_id=qro.id");
		sql.leftJoin("qr_sys_dept qdp", "qu.dept_id=qdp.id");
		sql.where("qu.init_default=0");
		Map<String, Object> where = new HashMap<>(6);
		sql.where("qu.group_id=#{group_id} or qu.id=#{group_id}");
		where.put("group_id", getUser().getUid());
		if (sysUserInfo.getDataSingle().getDept_id() != null) {
			sql.where("qu.dept_id=#{dept_id}");
			where.put("dept_id", sysUserInfo.getDataSingle().getDept_id());
		}
		if (sysUserInfo.getDataSingle().getRole_id() != null) {
			sql.where("qu.role_id=#{role_id}");
			where.put("role_id", sysUserInfo.getDataSingle().getRole_id());
		}
		if (!StringUtils.isEmpty(sysUserInfo.getDataSingle().getUser_number())) {
			sql.where("qu.user_number like '%#{user_number}%'");
			where.put("user_number", sysUserInfo.getDataSingle().getUser_number());
		}
		if (!StringUtils.isEmpty(sysUserInfo.getDataSingle().getUser_name())) {
			sql.where("(qu.user_name like '%#{user_name}%' or qu.real_name like '%#{user_name}%')");
			where.put("user_name", sysUserInfo.getDataSingle().getUser_name());
		}
		if (!StringUtils.isEmpty(sysUserInfo.getDataSingle().getMobile())) {
			sql.where("qu.mobile like '%#{mobile}%'");
			where.put("mobile", sysUserInfo.getDataSingle().getMobile());
		}
		sql.page(sysUserInfo.getPageIndex(), sysUserInfo.getPageSize());
		sql.orderBy("qu.id DESC");
		List<sys_user_info> sysUserInfosList = dbHelper.select(sql, where, sys_user_info.class);
		int uid = getUser().getUid();
		for (sys_user_info sysUserInfo1 : sysUserInfosList) {
			if(sysUserInfo1.getId()==uid){
				sysUserInfo1.setInit_default(1);
			}
		}
		where.clear();
		DataArray<sys_user_info> list = new DataArray<>();
		list.setDataList(sysUserInfosList);
		sql.clearColumns();
		sql.clearOrderBy();
		sql.select("count(*) as cnt").rows(null).offset(null);
		Map<String, Object> result = dbHelper.selectOne(sql);
		list.setDataCount(MapUtils.getInt(result, "cnt", 0));
		return list;
	}

	/**
	 * 添加用户
	 * 
	 * @param sysUserInfo
	 * @return
	 */
	public Integer addSysUser(sys_user_info sysUserInfo) {
		sysUserInfo.setGroup_id(getUser().getUid());
		Object i = dbHelper.insert("qr_sys_users", sysUserInfo, sys_user_info.class);
		return Integer.valueOf(i.toString());
	}

	/**
	 * 更新用户
	 * 
	 * @return
	 */
	public Integer updateSysUser(sys_user_info sysUserInfo) {
		sysUserInfo.setGroup_id(null);
		int i = dbHelper.update("qr_sys_users", sysUserInfo, sys_user_info.class, "id=#{id}");
		return i;
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public Integer deleteSysUser(sys_user_info sysUserInfo) {
		SQL.DeleteSQL sql = SQL.delete("qr_sys_users").where("id=#{id} and init_default=#{init_default} and id!=#{ix}")
			.addParameter("id", sysUserInfo.getId())
			.addParameter("ix", getUser().getUid())
			.addParameter("init_default", 0);
		int i = dbHelper.delete(sql);
		return i;
	}

}
